const math = {
  multiply(n1 = 0, n2 = 0) {
    const s1 = n1.toString();
    const s2 = n2.toString();
    const l1 = (s1.split('.')[1] || '').length;
    const l2 = (s2.split('.')[1] || '').length;
    const r1 = Number(s1.replace('.', ''));
    const r2 = Number(s2.replace('.', ''));
    return (r1 * r2) / Math.pow(10, l1 + l2);
  },
  divide(n1 = 0, n2 = 1) {
    const s1 = n1.toString();
    const s2 = n2.toString();
    const l1 = (s1.split('.')[1] || '').length;
    const l2 = (s2.split('.')[1] || '').length;
    const r1 = Number(s1.replace('.', ''));
    const r2 = Number(s2.replace('.', ''));
    return this.multiply(r1 / r2, Math.pow(10, l2 - l1));
  },
  add(n1 = 0, n2 = 0) {
    const s1 = n1.toString();
    const s2 = n2.toString();
    const l1 = (s1.split('.')[1] || '').length;
    const l2 = (s2.split('.')[1] || '').length;
    const c = Math.abs(l1 - l2);
    const m = Math.pow(10, Math.max(l1, l2));

    let r1 = 0;
    let r2 = 0;
    if (c > 0) {
      const cm = Math.pow(10, c);
      if (l1 > l2) {
        r1 = Number(s1.replace('.', ''));
        r2 = Number(s2.replace('.', '')) * cm;
      } else {
        r1 = Number(s1.replace('.', '')) * cm;
        r2 = Number(s2.replace('.', ''));
      }
    } else {
      r1 = Number(s1.replace('.', ''));
      r2 = Number(s2.replace('.', ''));
    }
    return (r1 + r2) / m;
  },
  subtract(n1 = 0, n2 = 0) {
    const l1 = (n1.toString().split('.')[1] || '').length;
    const l2 = (n2.toString().split('.')[1] || '').length;
    const m = Math.pow(10, Math.max(l1, l2));
    const n = l1 >= l2 ? l1 : l2;
    return Number(((n1 * m - n2 * m) / m).toFixed(n));
  },
};

module.exports = math;
